/**
 * Copyright 2013-2015 JueYue (qrb.jueyue@gmail.com)
 *   
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.afterturn.easypoi.test.excel.read.check;

import java.io.File;
import java.util.List;

import cn.afterturn.easypoi.test.entity.check.ImportCheckOneLine;
import cn.afterturn.easypoi.test.entity.check.ImportCheckOneLineNoAnn;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.util.PoiPublicUtil;

import org.junit.Assert;
import org.junit.Test;

/**
 * 输入模板校验 单行版本
 * 
 * @author JueYue 2015年11月13日 下午8:40:05
 */
public class ImportCheckTestOneLine {

	// start:**************************************************使用注释********************************************************************************
	/*
	 * 使用模板,一旦声明 isImportField = "true" 模板中该字段一定要有，不然报异常
	 */
	@Test
	public void testOneLine() {
		boolean isOK = true;
		try {
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			// 使用模板的话 需要在bean中 给需要导出的字段设置 isImportField = "true"
			List<ImportCheckOneLine> list = ExcelImportUtil.importExcel(new File(PoiPublicUtil.getWebRootPath("import/check.xls")), ImportCheckOneLine.class, params);
		} catch (Exception e) {
			e.printStackTrace();
			isOK = false;
		}
		Assert.assertTrue(isOK);
	}

	// 第二个sheet 少一个字段错误测试
	@Test
	public void testOneLineError() {
		boolean isOK = true;
		try {
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			params.setStartSheetIndex(1);// 开始读取sheet的位置，默认为0(第一个sheet)，这里读取的是第二个sheet
			List<ImportCheckOneLine> list = ExcelImportUtil.importExcel(new File(PoiPublicUtil.getWebRootPath("import/check.xls")), ImportCheckOneLine.class, params);
			for (ImportCheckOneLine importCheckOneLine : list) {
				System.out.println("姓名：" + importCheckOneLine.getName());
				// bean中声明 isImportField = "true"的话，模板中必须要有该字段，不然报异常
				// System.out.println("爱好："+importCheckOneLine.getHobby());
			}
		} catch (Exception e) {
			e.printStackTrace();
			isOK = false;
		}
		Assert.assertTrue(!isOK);
	}

	// end:****************************************************使用注释******************************************************************************

	// start:****************************************************不使用注释******************************************************************************

	// 不使用注释的方式(bean中无注释)
	@Test
	public void testOneLineByParams() {
		boolean isOK = true;
		try {
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			params.setImportFields(new String[] { "姓名", "性别", "年纪", "爱好" });
			List<ImportCheckOneLineNoAnn> list = ExcelImportUtil.importExcel(new File(PoiPublicUtil.getWebRootPath("import/check.xls")), ImportCheckOneLineNoAnn.class, params);
		} catch (Exception e) {
			e.printStackTrace();
			isOK = false;
		}
		Assert.assertTrue(isOK);
	}

	// 第二个sheet 少一个字段测试错误
	@Test
	public void testOneLineErrorByParams() {
		boolean isOK = true;
		try {
			ImportParams params = new ImportParams();
			params.setTitleRows(1);
			params.setStartSheetIndex(1);
			params.setImportFields(new String[] { "姓名", "性别", "年纪", "爱好" });
			List<ImportCheckOneLineNoAnn> list = ExcelImportUtil.importExcel(new File(PoiPublicUtil.getWebRootPath("import/check.xls")), ImportCheckOneLineNoAnn.class, params);
		} catch (Exception e) {
			e.printStackTrace();
			isOK = false;
		}
		Assert.assertTrue(!isOK);
	}

}
// end:****************************************************不使用注释******************************************************************************
